<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <h1>登录系统</h1>
        <p class="subtitle">欢迎回来，请选择用户类型并登录</p>
      </div>
      <form class="login-form" @submit.prevent="login">
        <div class="user-type-selector">
          <div 
            v-for="type in userTypes" 
            :key="type.value"
            :class="['type-item', { active: selectedUserType === type.value }]"
            @click="selectedUserType = type.value">
            <i :class="type.icon"></i>
            <span>{{ type.label }}</span>
          </div>
        </div>

        <div class="form-item">
          <label for="sno">
            <i class="el-icon-mobile-phone"></i>
          </label>
          <input type="text" id="sno" v-model="loginFormDTO.sno" placeholder="请输入学号">
        </div>
        <div class="form-item">
          <label for="password">
            <i class="el-icon-lock"></i>
          </label>
          <input type="password" id="password" v-model="loginFormDTO.password" placeholder="请输入密码" @keyup.enter="login">
        </div>
        <button type="submit" class="login-button">
          <span>登录</span>
          <i class="el-icon-right"></i>
        </button>
        <div class="extra-options">
          <!-- <a href="#" class="forgot-password">忘记密码？</a> -->
          <a href="#" class="register" @click="register">
            {{ getRegisterText() }}
          </a>
        </div>
      </form>
    </div>
    <div class="decoration-circles">
      <div class="circle circle-1"></div>
      <div class="circle circle-2"></div>
      <div class="circle circle-3"></div>
      <div class="circle circle-4"></div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      selectedUserType: 'student',
      userTypes: [
        { label: '普通学生', value: 'student', icon: 'el-icon-user', role: 1 },
        { label: '社联成员', value: 'association', icon: 'el-icon-user-solid', role: 2 },
        { label: '超级管理员', value: 'admin', icon: 'el-icon-s-custom', role: 3 }
      ],
      loginFormDTO:{
        sno: '',
        password: 'wang0112',
        role: 1  // 默认为普通学生
      }
    };
  },
  watch: {
    selectedUserType(newType) {
      // 根据选择的用户类型设置对应的role
      const userType = this.userTypes.find(type => type.value === newType);
      this.loginFormDTO.role = userType.role;
    }
  },
  methods: {
    register() {
      const registerRoutes = {
        student: 'register',
        association: 'association-register',
        admin: 'admin-register'
      };
      this.$router.push({name: registerRoutes[this.selectedUserType]});
    },
    getRegisterText() {
      return '快速注册';
    },
    login() {  
      // 统一使用一个登录接口
      axios.post('/api/user/login', this.loginFormDTO)
        .then(response => {
          if (response.data.code === 200) {
            // 保存token和用户信息
            localStorage.setItem("token", response.data.token);
            localStorage.setItem("user", JSON.stringify(response.data.user || {}));
            localStorage.setItem("role", response.data.user.role);
            const role = response.data.user.role;// 获取登录用户的角色
            if(role < this.loginFormDTO.role) {
              this.$message({
                showClose: true,
                message: '权限不足，无法登录!',
                type: 'error'
              });
              return;
            }
            this.$message({
              showClose: true,
              message: '登录成功!',
              type: 'success'
            });
                
            // 使用Vue Router进行跳转
            if (this.loginFormDTO.role === 3) {
              // 超级管理员
              this.$router.push({ path: '/admin/dashboard' });
            } else if (this.loginFormDTO.role === 2) {
              // 社联成员
              this.$router.push({ path: '/association/dashboard' });
            } else {
              // 普通用户
              this.$router.push({ path: '/index/home' });
            }
          } else {
            this.$message({
              showClose: true,
              message: response.data.msg,
              type: 'error'
            });
          }
        })
        .catch(err => {
          console.error('登录失败:', err);
          this.$message.error('登录失败，请稍后重试');
        });
    },
    handleLoginSuccess(userInfo) {
      // 存储用户信息到本地
      localStorage.setItem('user', JSON.stringify(userInfo));
      
      // 根据用户角色决定重定向的页面
      const role = userInfo.role;
      
      if (role === 3) {
        // 超级管理员跳转到管理员面板
        this.$router.push({ name: 'admin' });
      } else if (role === 2) {
        // 社联成员跳转到社联管理页面
        this.$router.push({ name: 'association' });
      } else {
        // 普通用户跳转到首页
        this.$router.push({ name: 'home' });
      }
    }
  }
};
</script>

<style scoped>
/* 重置默认样式 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.login-container {
  min-height: 100vh;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #4b6cb7 0%, #182848 100%);
  position: relative;
  overflow: hidden;
}

.login-card {
  width: 400px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
  z-index: 10;
  animation: fadeIn 0.8s ease;
  overflow: hidden;
  position: relative;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h1 {
  color: #333;
  font-size: 28px;
  margin-bottom: 10px;
  font-weight: 600;
}

.subtitle {
  color: #666;
  font-size: 14px;
}

.login-form {
  position: relative;
}

.form-item {
  margin-bottom: 25px;
  position: relative;
  display: flex;
  align-items: center;
  border-bottom: 2px solid #eee;
  transition: all 0.3s ease;
}

.form-item:focus-within {
  border-bottom-color: #4b6cb7;
}

.form-item label {
  width: 35px;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #999;
}

.form-item input {
  flex: 1;
  padding: 12px 10px;
  border: none;
  outline: none;
  background: transparent;
  color: #333;
  font-size: 16px;
}

.form-item input::placeholder {
  color: #aaa;
}

.login-button {
  width: 100%;
  padding: 15px;
  background: linear-gradient(to right, #4b6cb7, #182848);
  color: white;
  border: none;
  border-radius: 30px;
  cursor: pointer;
  font-size: 16px;
  font-weight: 500;
  margin-top: 20px;
  margin-bottom: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: all 0.3s ease;
  box-shadow: 0 5px 15px rgba(75, 108, 183, 0.4);
}

.login-button:hover {
  transform: translateY(-3px);
  box-shadow: 0 7px 20px rgba(75, 108, 183, 0.5);
}

.login-button span {
  margin-right: 8px;
}

.extra-options {
  display: flex;
  justify-content: center;
  margin-top: 15px;
  font-size: 14px;
}

.extra-options a {
  text-decoration: none;
  color: #4b6cb7;
  transition: all 0.3s ease;
  padding: 8px 16px;
  border-radius: 20px;
  background-color: rgba(75, 108, 183, 0.1);
}

.extra-options a:hover {
  color: white;
  background-color: #4b6cb7;
}

.decoration-circles {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  z-index: 1;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: linear-gradient(to right, rgba(75, 108, 183, 0.7), rgba(24, 40, 72, 0.7));
  filter: blur(5px);
}

.circle-1 {
  width: 200px;
  height: 200px;
  top: -100px;
  right: -50px;
  animation: float 6s ease-in-out infinite;
}

.circle-2 {
  width: 300px;
  height: 300px;
  bottom: -150px;
  left: -100px;
  animation: float 8s ease-in-out infinite;
}

.circle-3 {
  width: 150px;
  height: 150px;
  top: 40%;
  right: -75px;
  animation: float 7s ease-in-out infinite 1s;
}

.circle-4 {
  width: 120px;
  height: 120px;
  top: 20%;
  left: -60px;
  animation: float 5s ease-in-out infinite 2s;
}

@keyframes float {
  0% {
    transform: translateY(0) scale(1);
  }
  50% {
    transform: translateY(-20px) scale(1.05);
  }
  100% {
    transform: translateY(0) scale(1);
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式设计 */
@media (max-width: 500px) {
  .login-card {
    width: 90%;
    padding: 30px 20px;
  }
  
  .login-header h1 {
    font-size: 24px;
  }
  
  .circle-1, .circle-2, .circle-3, .circle-4 {
    opacity: 0.5;
  }
}

.user-type-selector {
  display: flex;
  justify-content: space-between;
  margin-bottom: 25px;
  padding: 5px;
  background: #f5f7fa;
  border-radius: 12px;
}

.type-item {
  flex: 1;
  padding: 12px;
  text-align: center;
  cursor: pointer;
  border-radius: 8px;
  transition: all 0.3s ease;
  color: #606266;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.type-item i {
  font-size: 24px;
}

.type-item span {
  font-size: 14px;
}

.type-item:hover {
  background: rgba(75, 108, 183, 0.1);
}

.type-item.active {
  background: #4b6cb7;
  color: white;
  box-shadow: 0 2px 8px rgba(75, 108, 183, 0.3);
}

@media (max-width: 500px) {
  .user-type-selector {
    flex-direction: column;
    gap: 10px;
  }
  
  .type-item {
    flex-direction: row;
    justify-content: center;
    padding: 8px;
  }
  
  .type-item i {
    margin-right: 8px;
  }
}
</style>
